package com.enation.app.shop.core.goods.plugin;

import com.enation.app.base.core.model.PluginTab;
import com.enation.app.shop.core.goods.plugin.search.IGetInsuranceAddHtmlEvent;
import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
import com.enation.framework.context.webcontext.ThreadContextHolder;
import com.enation.framework.plugin.AutoRegisterPluginsBundle;
import com.enation.framework.plugin.IPlugin;
import com.enation.framework.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("insurancePluginBundle")
public class InsurancePluginBundle  extends AutoRegisterPluginsBundle {
    @Override
    public String getName() {
        return "保险插件桩";
    }


    /**
     * 激发保险添加前事件
     * @param goods
     */
    public void onBeforeAdd(Map goods) {
        HttpServletRequest request = ThreadContextHolder.getHttpRequest();
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IInsuranceBeforeAddEvent) {
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件 : " + plugin.getClass() + " onBeforeGoodsAdd 开始...");
                    }
                    IInsuranceBeforeAddEvent event = (IInsuranceBeforeAddEvent) plugin;
                    event.onBeforeInsuranceAdd(goods, request);
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件 : " + plugin.getClass() + " onBeforeGoodsAdd 结束.");
                    }
                } else {
                    if (loger.isDebugEnabled()) {
                        loger.debug(" no,skip...");
                    }
                }
            }
        }
    }

    /**
     * 激发保险添加前事件
     * @param goods
     */
    public void onBeforeAddPreview(Map goods) {
        HttpServletRequest  request = ThreadContextHolder.getHttpRequest();
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IInsuranceBeforeAddEvent) {
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件 : " + plugin.getClass() + " onBeforeGoodsAdd 开始...");
                    }
                    IInsuranceBeforeAddEvent event = (IInsuranceBeforeAddEvent) plugin;
                    event.onBeforeInsuranceAdd(goods, request);
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件 : " + plugin.getClass() + " onBeforeGoodsAdd 结束.");
                    }
                } else {
                    if (loger.isDebugEnabled()) {
                        loger.debug(" no,skip...");
                    }
                }
            }
        }
    }

    /**
     * 激发保险添加完成后事件
     *
     * @param goods
     */
    public void onAfterAdd(Map goods) {

        HttpServletRequest  request = ThreadContextHolder.getHttpRequest();
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (loger.isDebugEnabled()) {
                    loger.debug("check plugin : " + plugin.getClass() + " is IGoodsAfterAddEvent ?");
                }

                if (plugin instanceof IInsuranceAfterEvent) {
                    if (loger.isDebugEnabled()) {
                        loger.debug(" yes ,do event...");
                    }
                    IInsuranceAfterEvent event = (IInsuranceAfterEvent) plugin;
                    try {
                        event.onAfterInsuranceAdd(goods, request);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                } else {
                    if (loger.isDebugEnabled()) {
                        loger.debug(" no,skip...");
                    }
                }
            }
        }
    }




    /**
     * 到添加保险页面，为此页填充input数据
     *
     */
    public List<PluginTab> onFillAddInputData() {

        List<PluginTab> list = new ArrayList<PluginTab>();
        HttpServletRequest request = ThreadContextHolder.getHttpRequest();

        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        freeMarkerPaser.putData("goods", new HashMap(0));
        //Integer typeid=0;
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGetInsuranceAddHtmlEvent) {
                    IGetInsuranceAddHtmlEvent event = (IGetInsuranceAddHtmlEvent) plugin;
                    freeMarkerPaser.setClz(event.getClass());
                    String html = event.getAddHtml(request);
                    if (!StringUtil.isEmpty(html)) {
                        if (plugin instanceof IInsuranceTabShowEvent) {

                            PluginTab tab = new PluginTab();

                            IInsuranceTabShowEvent tabEvent = (IInsuranceTabShowEvent) plugin;
                            String tabName = tabEvent.getTabName(66);

                            tab.setOrder(tabEvent.getOrder());
                            tab.setTabTitle(tabName);
                            tab.setTabHtml(html);

                            list.add(tab);
                        }
                    }
                }
            }
        }

        //对tab进行排序
        PluginTab.sort(list);

        return list;
    }


    /**
     * 到保险修改页面前为此页填充input数据
     *
     */
    public List<PluginTab> onFillEditInputData(Map goods) {
        List<PluginTab> list = new ArrayList<PluginTab>();
        HttpServletRequest request = ThreadContextHolder.getHttpRequest();
        FreeMarkerPaser freeMarkerPaser = FreeMarkerPaser.getInstance();
        freeMarkerPaser.putData("goods", goods);
        Integer typeid=64;
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGetInsuranceEditHtmlEvent) {
                    IGetInsuranceEditHtmlEvent event = (IGetInsuranceEditHtmlEvent) plugin;
                    freeMarkerPaser.setClz(event.getClass());
                    freeMarkerPaser.setPageName(null);
                    String html = event.getEditHtml(goods, request);
                    if (!StringUtil.isEmpty(html)) {
                        if (plugin instanceof IInsuranceTabShowEvent) {

                            PluginTab tab = new PluginTab();

                            IInsuranceTabShowEvent tabEvent = (IInsuranceTabShowEvent) plugin;
                            String tabName = tabEvent.getTabName(typeid);

                            tab.setOrder(tabEvent.getOrder());
                            tab.setTabTitle(tabName);
                            tab.setTabHtml(html);

                            list.add(tab);
                        }
                    }
                }
            }
        }
        //对tab进行排序
        PluginTab.sort(list);
        return list;
    }

    /**
     * 激发保险修改更新前事件
     *
     * @param goods
     *            页面传递的商品数据
     */
    public void onBeforeEdit(Map goods) {
        HttpServletRequest request = ThreadContextHolder.getHttpRequest();
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IInsuranceBeforeEditEvent) {
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件[" + plugin.getClass() + "] onBeforeGoodsEdit 开始...");
                    }
                    IInsuranceBeforeEditEvent event = (IInsuranceBeforeEditEvent) plugin;
                    event.onBeforeInsuranceEdit(goods, request);
                    if (loger.isDebugEnabled()) {
                        loger.debug("调用插件[" + plugin.getClass() + "] onBeforeGoodsEdit 结束.");
                    }
                }
            }
        }
    }

    /**
     * 激发保险修改后事件
     *
     * @param goods
     *            修改后的商品基本数据
     */
    public void onAfterEdit(Map goods) {
        HttpServletRequest request =ThreadContextHolder.getHttpRequest();
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IInsuranceAfterEditEvent) {
                    IInsuranceAfterEditEvent event = (IInsuranceAfterEditEvent) plugin;

                    try {
                        event.onAfterInsuranceEdit(goods, request);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }



                }
            }
        }
    }

    /**
     * lzf add 20120417
     *
     * @param goods
     */
    public void onVisit(Map goods) {
        List<IPlugin> plugins = this.getPlugins();
        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGoodsVisitEvent) {
                    IGoodsVisitEvent event = (IGoodsVisitEvent) plugin;
                    event.onVisit(goods);
                }
            }
        }
    }

    /**
     * 激发保险删除事件
     *
     * @param goodsid
     */
    public void onGoodsDelete(Integer[] goodsid) {
        List<IPlugin> plugins = this.getPlugins();
        if (plugins != null) {
            for (IPlugin plugin : plugins) {

                if (plugin instanceof IGoodsDeleteEvent) {
                    IGoodsDeleteEvent event = (IGoodsDeleteEvent) plugin;
                    event.onGoodsDelete(goodsid);
                }
            }
        }
    }

    /**
     * 激发获取select sql语句字串
     *
     * @return
     */
    public String onGetSelector() {
        StringBuffer sql = new StringBuffer();
        List<IPlugin> plugins = this.getPlugins();
        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGoodsBackendSearchFilter) {
                    IGoodsBackendSearchFilter event = (IGoodsBackendSearchFilter) plugin;
                    sql.append(event.getSelector());
                }
            }
        }
        return sql.toString();
    }

    public String onGetFrom() {
        StringBuffer sql = new StringBuffer();
        List<IPlugin> plugins = this.getPlugins();
        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGoodsBackendSearchFilter) {
                    IGoodsBackendSearchFilter event = (IGoodsBackendSearchFilter) plugin;
                    sql.append(event.getFrom());
                }
            }
        }
        return sql.toString();
    }

    public void onSearchFilter(StringBuffer sql) {
        List<IPlugin> plugins = this.getPlugins();

        if (plugins != null) {
            for (IPlugin plugin : plugins) {
                if (plugin instanceof IGoodsBackendSearchFilter) {
                    IGoodsBackendSearchFilter event = (IGoodsBackendSearchFilter) plugin;
                    event.filter(sql);
                }
            }
        }
    }
    /**
     * 保险变化，主用于商品生成静态页面后商品优惠需要更新商品信息
     * @param goods 商品
     */
    public void onStartchange(Map goods) {
        List<IPlugin> plugins = this.getPlugins();

        //判断插件列表是否为空
        if (plugins != null) {
            //循环插件列表
            for (IPlugin plugin : plugins) {

                //判断实现商品变化事件
                if (plugin instanceof IGoodsStartChange) {
                    //执行插件
                    IGoodsStartChange event = (IGoodsStartChange) plugin;
                    event.startChange(goods);
                }

            }

        }

    }

}
